
from pathlib import Path
import re

def regex_gunc(file, top=10):
    dict_ip = dict()
    dict_url = dict()
    path = Path().absolute() / file
    #  正则表达式分为两个部分：1.匹配ip  2.匹配以GET或POST开头的url
    regex = re.compile('''(?P<ip>(\d{1,3}\.){3}\d{1,3})|(?P<url>((?<="GET )[/:._a-zA-Z0-9]+)|((?<="POST )[/:._a-zA-Z0-9]+))''')

    with open(str(path)) as f:
        for line in f:                          # 以行为单位对数据进行提取
            w = regex.finditer(line)
            for i in w:
                ip = i.groupdict()['ip']
                url = i.groupdict()['url']
                if ip != None:
                    dict_ip[ip] = dict_ip.get(ip, 0) + 1
                if url != None:
                    dict_url[url] = dict_url.get(url, 0) + 1

    ret_ip = sorted(dict_ip.items(), key=lambda x:x[1], reverse=True)
    ret_url = sorted(dict_url.items(), key=lambda x:x[1], reverse=True)
    return ret_ip[:top], ret_url[:top]

ret = regex_gunc('access.log-20191119', 10)
print(ret[0], ret[1], sep='\n')

# ip匹配结果是对的，但是url匹配中一些特殊情况没有考虑到，例如/index.php和/index.php?a=1&b=2这样应该是同一个页面
# 但是正则中没有考虑到，可以尝试再修改一下代码